Pablo Garcia

Apps for iPhone, iPad, Apple TV and Apple Watch

Front Camera UIView

Jul 9, 2018

I’ve developed a FrontCameraView UIView class for a project that I’ve been working on ( Don't you think Luis? ;) ). This project has allowed me the opportunity to study Apple’s AVFoundation framework.

According to Apple documentation:

The AVFoundation framework combines four major technology areas that together encompass a wide range of tasks for capturing, processing, synthesising, controlling, importing and exporting audiovisual media on Apple platforms.

The main features of FrontCameraView class are:

  • Preview front camera
  • Move the preview UIView to every corner of the screen.
  • Record video

Configure camera and microphone permission

First of all, we need to ask the user for permission to use the camera and microphone. That’s why we need to configure our Info.plist. Include the NSCameraUsageDescription and NSMicrophoneUsageDescription key in your app’s Info.plist file. For each key we have to provide a message that explains to the user why our app needs to capture media.

This message will appear the first time we run the app and usually the user accepts it. But, what happens if the user has revoked this permission? I think it’s a good idea to check out if our app still has permission to access the camera and microphone. If not, we could show a warning message to the user stating that and offering a solution.

With this line of code we will show a warning message and, if the user presses the Settings button, send him to the app’s configuration panel.

CameraManager class

This class manage camera permissions, access, configuration, preview and recording.

This class has the following public functions:

  • previewFrontCamera
  • startRecording
  • stopRecording
  • cameraAuthorisation

FrontCameraView class

This class shows the camera preview and allows the user to move the view to every corner of the screen and start and stop recording the camera preview.

This class has the following public functions:

  • starPreview
  • stopPreview
  • startRecording
  • stopRecording

FrontCameraView in Action

You can download the full project with a fully functional example here

And that’s it!! If you use it I’ll be happy to know about it.